package servlet;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

@WebListener
public class ThymeleafListener implements ServletContextListener {
    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent) {
        TemplateEngine engine=new TemplateEngine();
        ServletContextTemplateResolver resolver=new ServletContextTemplateResolver(servletContextEvent.getServletContext());
        resolver.setPrefix("/WEB-INF/template/");
        resolver.setSuffix(".html");
        resolver.setCharacterEncoding("utf8");
        engine.setTemplateResolver(resolver);
        servletContextEvent.getServletContext().setAttribute("engine",engine);
        System.out.println("Thymeleaf 初始化完毕！");

    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}
